package cn.hill4j.tool.spring.ext.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
/**
  * ClassName: SpringBeanUtils
  * Description: spring bean工具类
  * @author hillchen
  * Date: 2023/8/13
  * @version: 1.0
  */
@Configuration
public class SpringBeanUtils implements ApplicationContextAware {
    private static ApplicationContext CONTEXT;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CONTEXT = applicationContext;
    }
    /**
     * @author hillchen
     * Description: 获取bean
     * Date: 2023/8/13 00:51
     * @param <T> bean类型
     * @param beanName bean名称
     * @param clazz bean类型
     * @return T
     **/
    public static <T> T getBean(String beanName,Class<T> clazz){
        return CONTEXT.getBean(beanName,clazz);
    }

    /**
     * @author hillchen
     * Description: 获取bean
     * Date: 2023/8/13 00:51
     * @param <T> bean类型
     * @param beanName bean名称
     * @return T
     **/
    public static <T> T getBean(String beanName){
        return (T)CONTEXT.getBean(beanName);
    }

    /**
     * @author hillchen
     * Description: 获取bean
     * Date: 2023/8/13 00:51
     * @param <T> bean类型
     * @param clazz bean类型
     * @return T
     **/
    public static <T> T getBean(Class<T> clazz){
        return (T)CONTEXT.getBean(clazz);
    }
}
